#ifndef _NOTIFIER_H
#define _NOTIFIER_H
#include <qobject>
#include <QSqlDatabase>
#include <QThread>
#include <QSystemTrayIcon>
#include <QSqlTableModel>
#include <QTextDocument>
#include <QStyledItemDelegate>
#include <QMutex>
#include <QLabel>
#include <QWaitCondition>
#include <QDateTime>
#include "ui_notifier.h"
#include "wpxmlrpc.h"

class CommentItemDelegate : public QStyledItemDelegate
{
public:
	CommentItemDelegate(QObject* parent = NULL);
	~CommentItemDelegate();
	 void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
	 QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
	 QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
	 {
		 return NULL;
	 }

	 const int m_padding;
};
//class CommentsModel : public QSqlTableModel
//{
//	Q_OBJECT
//public:
//	CommentsModel( QObject * parent = 0, QSqlDatabase db = QSqlDatabase()) : QSqlTableModel(parent, db)
//	{
//	}
//	~CommentsModel()
//	{
//	}
//
//	QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const 
//	{
//		QModelIndex index = QSqlTableModel::index(row, column, parent);
//		return index;
//	}
//
//	QTextDocument* getDocument(const QModelIndex& index);
//	
//};

class CommentsModel : public QAbstractListModel
{
	Q_OBJECT
public:
	class CommentItem
	{
	public:
		Comment* comment;
		QTextDocument doc;
		QRect rcAuthor;
		QRect rcPostTitle;
	};
	enum Status{approve, hold, spam};
	CommentsModel( QObject * parent = 0);
	~CommentsModel();
	bool getComments(const BlogSite& site, Status status, int offset, int number, int postid = 0);
	QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const ;
	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
	bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
	protected slots:
		void getCommentsSuccess();
protected:
	
	QString statusToString(Status status);
	QList<CommentItem*> m_comments;
	friend class CommentItemDelegate;
};

class CheckCommentsThread : public QThread
{
	Q_OBJECT
public:
	CheckCommentsThread(QObject* parent);
	~CheckCommentsThread();
	void checkSite(const BlogSite& site);
	void run();
protected:
	bool m_bQuit;
	 QMutex mutex;
     QWaitCondition cond;
	 BlogSite m_site;
};

class Option
{
public:
	Option(){Init();}
	void Init()
	{
		m_pageSize = 20;
	}
	int m_pageSize;
};

class NotifierWnd : public QWidget
{
	Q_OBJECT
public:
	NotifierWnd(QWidget* parent);
	~NotifierWnd();
	protected slots:
		void itemEntered(const QModelIndex&);
		void trayActivated(QSystemTrayIcon::ActivationReason);
		void checkNewComments();
		void closeProgram();
		void setChecked(bool);
		void doNewComment();
		void doCommand(const QString&);
		void doRefresh();
		void doConfig();
		void modelReset();
		void currentRowChanged(const QModelIndex &, const QModelIndex &);
		//void checkComments(BlogSite* site);
protected:
	void createTray();
	void closeEvent(QCloseEvent *e);

	Ui::Notifier ui_;
	CommentsModel* m_model;
	CommentItemDelegate* m_delegate;
	QLabel* m_toolbar;

	QMenu* m_trayMenu;
	
	QSystemTrayIcon* m_tray;
	QList<BlogSite*> m_sites;
	QTimer* m_refreshTimer;

	//QModelIndex m_replyIndex;
	Comment* m_replyComment;

	QToolButton* tb_reply, tb_delete, tb_approve, tb_edit, tb_spam,tb_upapprove;
};

#endif